package com.example.stock.service.impl;

import com.example.stock.excel.ETFConstruct;
import com.example.stock.excel.ExcelReaderUtil;
import com.example.stock.service.EtfConstructService;
import com.example.stock.util.DataUtil;
import jakarta.annotation.PostConstruct;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class EtfConstructServiceImpl implements EtfConstructService {

    private Map<String, List<ETFConstruct>> listGroupByETF;

    private Map<String, List<ETFConstruct>> listGroupByStock;

    @PostConstruct
    private void init() {
        List<ETFConstruct> list = DataUtil.getListData(ETFConstruct.class, ExcelReaderUtil.FILE_ETF_CONSTRUCT);

        listGroupByStock = list.stream().collect(Collectors.groupingBy(ETFConstruct::getStock));

        listGroupByETF = list.stream().collect(Collectors.groupingBy(ETFConstruct::getEtf));
    }

    @Override
    public List<ETFConstruct> getETFConstructByName(String etfName) {
        return listGroupByETF.get(etfName);
    }

    @Override
    public List<ETFConstruct> getETFNamesByStock(String stock) {
        return listGroupByStock.get(stock);
    }
}